package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.ShopProtocolDto;
import com.lzy.cloud.base.facade.ShopProtocolFacade;
import com.lzy.cloud.base.request.ShopProtocolRequest;
import com.lzy.cloud.base.response.ShopProtocolResponse;
import com.lzy.cloud.base.response.UserToken;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商家协议
 *
 * @author lc
 * @version 2018/8/17
 */
@RestController
@RequestMapping("/shop/protocol")
public class ShopProtocolController {

    @Autowired
    private BaseController baseController;

    @Autowired
    private ShopProtocolFacade shopProtocolFacade;

    /**
     * 增@RequestHeader("token")String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create( @RequestBody ShopProtocolRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        Result userLogin = baseController.isUserLogin(token);
//        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
//        request.setShopId(userToken.getPlatformId());
        boolean ok = shopProtocolFacade.create(request);
        return new Result(ok);
    }

    /**
     * 分页查询@RequestHeader("token")String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result selectList(ShopProtocolRequest request) {
        Ensure.that(request).isNotNull("参数为空");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<ShopProtocolDto> pageInfo = shopProtocolFacade.selectList(pager, DozerUtils.map(request, ShopProtocolDto.class));
        pager.setTotal(Long.valueOf(pageInfo.getList().size()));
        return new Result(pager,DozerUtils.maps(pageInfo.getList(),ShopProtocolResponse.class));
    }

    /**
     * 编辑@RequestHeader("token")String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update( @RequestBody ShopProtocolRequest request) {
        Ensure.that(request).isNotNull("00000002");
//        Result userLogin = baseController.isUserLogin(token);
//        UserToken userToken = DozerUtils.map(userLogin.getResult(), UserToken.class);
//        request.setShopId(userToken.getPlatformId());
        Boolean ok = shopProtocolFacade.update(request);
        return new Result(ok);
    }

}
